package com.lb.videoplay.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lb.videoplay.MainApplication;
import com.lb.videoplay.R;
import com.lb.videoplay.fragment.HomeFragment;
import com.lb.videoplay.fragment.MeFragment;
import com.lb.videoplay.model.UserInfoModel;
import com.lb.videoplay.statuslib.StatusUtil;
import com.lb.videoplay.utils.SharedPref;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by Mr.lb
 * 时间： 2018/11/15
 * 包名： com.lb.videoplay.activity
 * 描述： 首页
 */
public class MainActivity extends FragmentActivity implements View.OnClickListener {

    private ImageView mIvHome, mIvMe;
    private TextView mTvHome, mTvMe;
    private LinearLayout mLlHome, mLlMe;

    public Fragment mHomeFragment = null, mMeFragment = null, mCurrentFragment = null;
    private int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;


    protected void setStatus() {
        StatusUtil.setUseStatusBarColor(this, Color.TRANSPARENT, StatusUtil.USE_DEFAULT_COLOR);
        StatusUtil.setSystemStatus(this, true, true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            boolean result = fixOrientation();
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setStatus();
        initWidget();
        initTab();
        initEvent();
    }

    //判断8.0
    private boolean isTranslucentOrFloating(){
        boolean isTranslucentOrFloating = false;
        try {
            int [] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
            final TypedArray ta = obtainStyledAttributes(styleableRes);
            Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
            m.setAccessible(true);
            isTranslucentOrFloating = (boolean)m.invoke(null, ta);
            m.setAccessible(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isTranslucentOrFloating;
    }

    private boolean fixOrientation(){
        try {
            Field field = Activity.class.getDeclaredField("mActivityInfo");
            field.setAccessible(true);
            ActivityInfo o = (ActivityInfo)field.get(this);
            o.screenOrientation = -1;
            field.setAccessible(false);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void setRequestedOrientation(int requestedOrientation) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            return;
        }
        super.setRequestedOrientation(requestedOrientation);
    }



    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            mMeFragment = new MeFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.content, mMeFragment).commit();
            changeFragment(transaction, mMeFragment, 1);
        }
    }

    public void initEvent() {
        mLlHome.setOnClickListener(this);
        mLlMe.setOnClickListener(this);
    }

    public void initWidget() {
        mTvHome = findViewById(R.id.tv_home);
        mTvMe = findViewById(R.id.tv_me);
        mIvHome = findViewById(R.id.iv_home);
        mIvMe = findViewById(R.id.iv_me);
        mLlHome = findViewById(R.id.ll_home);
        mLlMe = findViewById(R.id.ll_me);
    }

    //初始化Tab
    public void initTab() {
        if (mHomeFragment == null) {
            mHomeFragment = new HomeFragment();
            mTvHome.setSelected(true);
            mIvHome.setSelected(true);
            getSupportFragmentManager().beginTransaction().add(R.id.content, mHomeFragment).commit();
            mCurrentFragment = mHomeFragment;
        }
    }

    //tab 切换跳转
    public void changeFragment(FragmentTransaction transaction, Fragment fragment, int index) {
        if (mCurrentFragment == fragment)
            return;
        transaction.hide(mCurrentFragment).show(fragment).commit();
        mCurrentFragment = fragment;
        changeTabView(index);
    }

    //显示当前tabview
    private void changeTabView(int index) {
        mIvHome.setSelected(false);
        mTvHome.setSelected(false);
        mTvMe.setSelected(false);
        mIvMe.setSelected(false);
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.soon_in);
        switch (index) {
            case 0:
                mIvHome.setSelected(true);
                mTvHome.setSelected(true);
                mIvHome.startAnimation(animation);//开始动画
                break;
            case 1:
                mTvMe.setSelected(true);
                mIvMe.setSelected(true);
                mIvMe.startAnimation(animation);//开始动画
                break;
        }
    }

    //主页返回键 点击弹出dialog
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            showMessagePositiveDialog();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    //退出dialog
    private void showMessagePositiveDialog() {
        new QMUIDialog.MessageDialogBuilder(MainActivity.this)
                .setMessage(getString(R.string.app_himt_sign))
                .addAction(getString(R.string.app_cancel), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(getString(R.string.app_confirm), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        finish();
                    }
                })
                .create(mCurrentDialogStyle).show();
    }

    @Override
    public void onClick(View view) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (view.getId()) {
            case R.id.ll_home://首页
                if (mHomeFragment == null) {
                    mHomeFragment = new HomeFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.content, mHomeFragment).commit();
                }
                changeFragment(transaction, mHomeFragment, 0);
                break;
            case R.id.ll_me://我的
                if (mMeFragment == null) {
                    mMeFragment = new MeFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.content, mMeFragment).commit();
                }
                changeFragment(transaction, mMeFragment, 1);
                break;
        }
    }
}
